package com.boco.modules.fdoc.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.dao.DoctorDao;
import com.boco.modules.fdoc.dao.DoctorUserDao;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.DoctorUserEntity;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;

@Service

public class DocServiceImpl implements DocService {
	
	@Resource
	DoctorDao doctorDao;
	@Resource
	DoctorUserDao docUserDao;

	@Override
	public DoctorDetailVo getDoctorInfo(String id) {
		return doctorDao.getDoctorInfo(id);
	}


	@Override
	public DoctorEntity getDocEntity(String id) {
		return doctorDao.getDocEntity(id);
	}
	
	@Override
	public List<DoctorDetailVo> getDoctorTeamMemberByTeamId(String teamId) {
		return doctorDao.getDoctorTeamMemberByTeamId(teamId);
	}

	@Override
	public List<DoctorDetailVo> getDoctorTeamMemberByDocId(String docId) {
		return doctorDao.getDoctorTeamMemberByDocId(docId);
	}

	@Override
	public DoctorDetailVo getTeamLeaderInfo(String teamId) {
		return doctorDao.getTeamLeaderInfo(teamId);
	}

	@Override
	public List<DoctorDetailVo> getDoctorList(String docName,
			Integer pageSize, Integer pageNo) {
		// 设置查询参数
		DoctorDetailVo vo = new DoctorDetailVo();
		vo.setDocName(docName);
		// 设置分页对象
		Page<DoctorDetailVo> page = new Page<DoctorDetailVo>(pageNo,pageSize);
		vo.setPage(page);
		return doctorDao.getDoctorList(vo);
	}

	@Override
	public Integer getDoctorCount(DoctorDetailVo docVo) {
		// 设置查询参数
		return doctorDao.getDoctorCount(docVo);
	}

	@Override
	public String getRandomProductCode() {
		return doctorDao.getRandomProductCode();
	}

	@Override
	public Integer getTeamCount() {
		return doctorDao.getTeamCount();
	}

	@Override
	public List<String> getTeamIdsByHospital(String orgId) {
		return doctorDao.getTeamIdsByHospital(orgId);
	}

	@Override
	public List<DoctorDetailVo> getDocList(DoctorDetailVo docVo) {
		return doctorDao.getDocList(docVo);
	}

	@Override
	public int saveDoctor(DoctorEntity docEntity) {
		return doctorDao.saveDoctor(docEntity);
	}

	@Override
	public int deleteDoc(String id) {
		
		int flag=0;
		flag=doctorDao.deleteDoc(id);
		DoctorUserEntity doctorUserEntity=docUserDao.getBaseInfo(id);
		if(doctorUserEntity!=null){
			flag=docUserDao.deleteDocUser(id);
		}
		return flag;
	}

	@Override
	public int updateDoctor(DoctorEntity docEntity) {
		return doctorDao.update(docEntity);
	}

	@Override
	public List<DoctorDetailVo> getLeaderList(DoctorDetailVo docVo) {
		return doctorDao.getLeaderList(docVo);
	}

	@Override
	public int getLeaderCount(DoctorDetailVo countVo) {
		return doctorDao.getLeaderCount(countVo);
	}

	@Override
	public int deleteTeam(String teamId) {
		return doctorDao.deleteTeam(teamId);
	}

	@Override
	public List<DoctorDetailVo> getDocByType(DoctorDetailVo docVo) {
		return doctorDao.getDocByType(docVo);
	}

	@Override
	@Transactional
	public int saveTeam(DoctorEntity docEntity) {
		int flag1=doctorDao.diseaseLabelReset(docEntity.getTeamId());
		int flag2=doctorDao.saveTeam(docEntity);
		if(flag1>0 && flag2==1){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public int clearTeamId(String teamId) {
		return doctorDao.clearTeamId(teamId);
	}

	@Override
	@Transactional
	public int saveDoctorAndUser(DoctorEntity docEntity, DoctorUserEntity userEntity) {
		
		
		DoctorEntity doctorEntity=doctorDao.getDocEntity(docEntity.getId());
		DoctorUserEntity doctorUserEntity=docUserDao.getUserByUserId(userEntity.getDoctorUserId());
		DoctorUserEntity doctorUserEntity2=docUserDao.getBaseInfo(userEntity.getDoctorId());
		Integer flag1=1;
		Integer flag2=1;
		
		if(doctorEntity==null){
			flag1=doctorDao.insert(docEntity);
		}else{
			flag1=doctorDao.update(docEntity);
		}
		
		if(doctorUserEntity2 !=null){
			flag2=docUserDao.updateUser(userEntity);
		}else if(doctorUserEntity !=null){
			flag2=docUserDao.updateById(userEntity);
		}else{
			flag2=docUserDao.insert(userEntity);
		}
		/*if(doctorEntity == null && doctorUserEntity == null){
			flag1=doctorDao.insert(docEntity);
			flag2=docUserDao.insert(userEntity);
		}
		if(doctorEntity != null && doctorUserEntity ==null){
			flag1=doctorDao.update(docEntity);
			flag2=docUserDao.insert(userEntity);
		}
		if(doctorEntity == null && doctorUserEntity !=null){
			flag1=doctorDao.insert(docEntity);
			flag2=docUserDao.updateById(userEntity);
		}*/
		
		if(flag1==1 && flag2==1){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	@Transactional
	public Integer updateTeam(List<DoctorDetailVo> docList, List<Map> returnList, String teamId) {
		int i=1,j=1,k=1;
		for(DoctorDetailVo dVo:docList){
			int flag=0;
			for(Map m1:returnList){
				if(dVo.getId().equals(m1.get("docId").toString())){
					DoctorEntity dEntity=new DoctorEntity();
					dEntity.setId(m1.get("docId").toString());
					if(((String)m1.get("leaderFlag"))!="" && ((String)m1.get("leaderFlag"))!=null){
						dEntity.setLeaderFlag((String)m1.get("leaderFlag"));
					}else{
						dEntity.setLeaderFlag(null);
					}
					dEntity.setUpdateTime(new Date());
					dEntity.setTeamId(teamId);
				
					i=doctorDao.updateTeam(dEntity);
					flag=1;
					break;
				}
			}
			if(flag==0){
				DoctorEntity dEntity=new DoctorEntity();
				dEntity.setId(dVo.getId());
				dEntity.setTeamId(null);
				dEntity.setLeaderFlag(null);
				dEntity.setUpdateTime(new Date());
				j=doctorDao.updateTeam(dEntity);
				
			}
		}
		
		for(Map m2:returnList){
			int flag=0;
			for(DoctorDetailVo dVo:docList){
				if(dVo.getId().equals(m2.get("docId").toString())){
					flag=1;
					break;
				}
			}
			if(flag==0){
				String id=(String) m2.get("docId");
				DoctorEntity doEntity=doctorDao.getDocEntity(id);
				if("1".equals(doEntity.getLeaderFlag())){
					return -1;
				}
				
				DoctorEntity dEntity=new DoctorEntity();
				dEntity.setTeamId(teamId);
				if(((String)m2.get("leaderFlag"))!="" && ((String)m2.get("leaderFlag"))!=null){
					dEntity.setLeaderFlag((String)m2.get("leaderFlag"));
				}else{
					dEntity.setLeaderFlag(null);
				}
				dEntity.setUpdateTime(new Date());
				dEntity.setId(m2.get("docId").toString());
				
				k=doctorDao.updateTeam(dEntity);
			}
		}
		
		if(i==1 && j==1 && k==1){
				return 1;
		}else{
			return 0;
		}
	}


	

	
}
